package event

import (
	"context"
	"encoding/json"
	"time"

	"github.com/google/uuid"
)

type EventData struct {
	ID      string    `json:"id"`
	Source  string    `json:"source"`
	Type    string    `json:"type"`
	Data    any       `json:"data"`
	Time    time.Time `json:"time"`
	Subject string    `json:"subject"`
}

func NewEventData(subject, event string, data any) EventData {
	return EventData{
		ID:      uuid.NewString(),
		Source:  "monitor",
		Type:    event,
		Data:    data,
		Time:    time.Now(),
		Subject: subject,
	}
}

func (e EventData) ToJson() []byte {
	b, _ := json.Marshal(e)
	return b
}

type Event interface {
	Key() string
	Value() []byte
}

type Handler func(context.Context, string, Event) error

type Sender interface {
	Send(ctx context.Context, topic string, msg Event) error
	Close() error
}

type Receiver interface {
	Receive(ctx context.Context, handler Handler) error
	Close() error
}
